package cn.jimmie.learn.viewArt.weight

import android.content.Context
import android.graphics.*
import android.os.Build
import android.support.annotation.RequiresApi
import android.util.AttributeSet


/**
 * function :
 *
 * <p> Create Time:  2019年01月09日 10:32 </p>
 * <p> @author Jimmie.Qian  </p>
 */

class XferModeView(context: Context, attrs: AttributeSet) : BaseView(context, attrs) {
    private val xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)

    val path = Path()

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onDraw(canvas: Canvas) {
        drawLayer(canvas)
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    private fun drawLayer(canvas: Canvas) {
        val halfW = width / 2f
        val halfH = height / 2f

        val count = canvas.saveLayer(0f, 0f, width / 2f, height / 2f, paint)
        paint.color = Color.RED
        canvas.drawCircle(halfW / 2, halfH / 2, halfH / 2, paint)
        paint.xfermode = xfermode
        paint.color = Color.BLUE
        canvas.drawRect(halfW / 2, halfH / 2, halfW / 2 * 3, halfH / 2 * 3, paint)
        paint.xfermode = null
        canvas.restoreToCount(count)
    }

    private fun drawClipFail(canvas: Canvas) {
        val halfW = width / 2f
        val halfH = height / 2f

        canvas.clipRect(0f, 0f, width / 2f, height / 2f)
        paint.color = Color.RED
        canvas.drawCircle(halfW / 2, halfH / 2, halfH / 2, paint)
        paint.xfermode = xfermode
        paint.color = Color.BLUE
        canvas.drawRect(halfW / 2, halfH / 2, halfW / 2 * 3, halfH / 2 * 3, paint)
        paint.xfermode = null
    }

    private fun drawClipFail2(canvas: Canvas) {
        val halfW = width / 2f
        val halfH = height / 2f

        paint.color = Color.RED
        canvas.drawCircle(halfW / 2, halfH / 2, halfH / 2, paint)

        canvas.clipRect(halfW / 2, halfH / 2, halfW / 2 * 3, halfH / 2 * 3)
        canvas.drawColor(Color.BLUE, PorterDuff.Mode.SRC_IN)
    }

    private fun drawClipFail3(canvas: Canvas) {
        val halfW = width / 2f
        val halfH = height / 2f

        paint.color = Color.RED
        canvas.drawCircle(halfW / 2, halfH / 2, halfH / 2, paint)
        paint.xfermode = xfermode
        paint.color = Color.BLUE
        canvas.drawRect(halfW / 2, halfH / 2, halfW / 2 * 3, halfH / 2 * 3, paint)
        paint.xfermode = null
    }


    private fun drawClipSuccess(canvas: Canvas) {
        val halfW = width / 2f
        val halfH = height / 2f

        path.reset()
        path.addCircle(halfW / 2, halfH / 2, halfH / 2, Path.Direction.CW)

        canvas.clipPath(path)
        canvas.drawColor(Color.RED)
        paint.color = Color.BLUE
        canvas.drawRect(halfW / 2, halfH / 2, halfW / 2 * 3, halfH / 2 * 3, paint)
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    private fun drawClipSuccess2(canvas: Canvas) {
        val halfW = width / 2f
        val halfH = height / 2f

        val count = canvas.saveLayer(0f, 0f, width / 2f, height / 2f, paint)
        paint.color = Color.RED
        canvas.drawCircle(halfW / 2, halfH / 2, halfH / 2, paint)

        canvas.clipRect(halfW / 2, halfH / 2, halfW / 2 * 3, halfH / 2 * 3)
        canvas.drawColor(Color.BLUE, PorterDuff.Mode.SRC_IN)
        canvas.restoreToCount(count)
    }

    private fun drawClipSuccess3(canvas: Canvas) {
        val halfW = width / 2f
        val halfH = height / 2f

        val bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        val localCanvas = Canvas(bm)

        canvas.clipRect(0f, 0f, width / 2f, height / 2f)
        paint.color = Color.RED
        localCanvas.drawCircle(halfW / 2, halfH / 2, halfH / 2, paint)
        paint.xfermode = xfermode
        paint.color = Color.BLUE
        localCanvas.drawRect(halfW / 2, halfH / 2, halfW / 2 * 3, halfH / 2 * 3, paint)
        paint.xfermode = null

        canvas.drawBitmap(bm, 0f, 0f, null)
    }
}